package com.lingyi.insectmanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lingyi.insectmanagementsystem.pojo.Insect;
import com.lingyi.insectmanagementsystem.pojo.InsectCategory;
import com.lingyi.insectmanagementsystem.service.InsectCategoryService;
import com.lingyi.insectmanagementsystem.service.InsectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @auth 吴阳基
 * @date 2024/2/16 21:20
 */
@Controller
@RequestMapping("/insect")
public class InsectCategoryController {
	@Autowired
	private InsectService insectService;
	
	@Autowired
	private InsectCategoryService insectCategoryService;
	
	
	@GetMapping("/list")
	public String list(Model model, Insect insect) {
		
		QueryWrapper<Insect> insectQueryWrapper = new QueryWrapper<>();
		
		if (insect.getCid() != null && insect.getCid() > 0) {
			insectQueryWrapper.eq("cid", insect.getCid());
		}
		if (insect.getName() != null && insect.getName() != "") {
			insectQueryWrapper.like("name", insect.getName());
		}
		
		List<Insect> all = insectService.list(insectQueryWrapper);
		
		List<InsectCategory> allType = insectCategoryService.list();
		
		
		model.addAttribute("all", all);
		model.addAttribute("allType", allType);
		
		
		return "main";//转发
	}
	
	@GetMapping("/toAdd")
	public String toAdd(Model model) {
		List<InsectCategory> allType = insectCategoryService.list();
		
		model.addAttribute("allType", allType);
		
		return "add";
	}
	
	/**
	 * 新增
	 *
	 * @param insect
	 * @return
	 */
	@PostMapping("/add")
	public String add(Insect insect) {
		
		boolean save = insectService.save(insect);
		
		if (save) {
			return "redirect:list";//重定向
		}
		
		return "add";
	}
	
	
	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	@ResponseBody
	public boolean delete(Integer id) {
		return insectService.removeById(id);
	}
	
	
	/**
	 * 查询单条记录
	 *
	 * @param model
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@GetMapping("/byId")
	public String byId(Model model, @RequestParam("id") Integer id) {
		Insect insect = insectService.getById(id);
		
		List<InsectCategory> allType = insectCategoryService.list();
		
		model.addAttribute("allType", allType);
		
		model.addAttribute("insect", insect);
		
		return "update";
		
	}
	
	
	/**
	 * 更新
	 *
	 * @param insect
	 * @return
	 */
	@PostMapping("/update")
	public String update(Insect insect) {
		

		UpdateWrapper<Insect> insectUpdateWrapper = new UpdateWrapper<>();
		insectUpdateWrapper.eq("id", insect.getId()) // 设定更新条件
				.set("name", insect.getName())        // 需要更新的字段
				.set("life", insect.getLife())
				.set("area", insect.getArea())
				.set("discoveryTime", insect.getDiscoveryTime())
				.set("cid", insect.getCid());
		boolean result = insectService.update(insectUpdateWrapper);
		
		if (result) {
			return "redirect:list";//重定向
		}
		return "update";
	}
	
}
